package com.magicsoft.share.constant;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.magicsoft.share.customview.alertview.AlertView;
import com.magicsoft.share.customview.alertview.OnItemClickListener;
import com.magicsoft.share.rx_retrofit_network.NetWorkManager;
import com.magicsoft.share.rx_retrofit_network.location.APIExceptionCallBack;
import com.magicsoft.share.rx_retrofit_network.location.model.BaseResponse;
import com.magicsoft.share.ui.main.LoginActivity;
import com.magicsoft.share.utils.SPUtils;

/**
 * Created by lyf on 2017/10/26/026.
 */

public class ShareApplication extends Application {
    private AlertView alertView;
    private Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(final Activity activity) {
                alertView = new AlertView.Builder().setContext(activity).setTitle("检测到您尚未登录或登录过期，是否现在登录？").setStyle(AlertView.Style.Alert).setCancelText("否").setOthers(new String[]{"是"}).setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(Object o, int position) {

                        if (position == -1) {
                            alertView.dismiss();

                        } else {
                            alertView.dismiss();
                            Intent intent = new Intent(activity, LoginActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);

                        }
                    }
                }).build();
            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });

        SPUtils.init(this);
        initDialog();

        NetWorkManager.init(Url.BASE_URL, 900000, this);

        NetWorkManager.setOpenApiException(true);
        NetWorkManager.putApiCallback(new APIExceptionCallBack() {
            @Override
            public String callback(BaseResponse baseResponse) {
                Log.d("lyf", "callback: ");
                alertView.show();
                return null;
            }

        }, 999999);
        NetWorkManager.putApiCallback(new APIExceptionCallBack() {
            @Override
            public String callback(BaseResponse baseResponse) {
                Log.d("lyf", "callback: ");
                alertView.show();
                return null;
            }

        }, 999998);
    }

    private void initDialog() {


    }
}
